package com.zhangyj.entitis;

import java.util.ArrayList;
import java.util.List;

public class DataSet {
	private List<Object[]> rows = new ArrayList<Object[]>();
	private Object[] row = null;
	private int pos = 0;
	private int colnum = 0;

	public DataSet(int colNum) {
		this.colnum = colNum;
	}

	public boolean next() {
		pos++;
		if (pos < rows.size()) {
			row = rows.get(pos);
			return true;
		} else {
			return false;
		}
	}

	int getInt(int colIndex) {
		return (Integer) row[colIndex];
	}

	double getDouble(int colIndex) {
		return (Double) row[colIndex];
	}

	String getString(int colIndex) {
		return (String) row[colIndex];
	}

	void setInt(int colIndex, int val) {
		row[colIndex] = val;
	}

	void setDouble(int colIndex, double val) {
		row[colIndex] = val;
	}

	void setString(int colIndex, String val) {
		row[colIndex] = val;
	}

	void append() {
		row = new Object[colnum];
		rows.add(row);
	}

	void remove(int rowIndex) {
		rows.remove(rowIndex);
		if (this.pos == rowIndex) {
			this.pos--;
		}
		if (rows.size() > this.pos) {
			row = rows.get(this.pos);
		} else {
			row = null;
		}

	}

	public int getPos() {
		return pos;
	}

	public void setPos(int pos) {
		this.pos = pos;
		row = rows.get(this.pos);
	}

	public int getColnum() {
		return colnum;
	}

	public void setColnum(int colnum) {
		int srcLeng = -1;
		if (this.colnum != colnum) {
			srcLeng = this.colnum > colnum ? colnum:this.colnum;
		}
		for (int i = 0; i < rows.size(); i++) {
			Object [] row = rows.get(i);
			Object [] newRow = new Object[colnum];
			System.arraycopy(row, 0, newRow, 0, srcLeng);
		}
		this.colnum = colnum;
	}
	
}
